package com.chexiehui.jc.mvp.mine;

import com.blankj.utilcode.util.StringUtils;
import com.chexiehui.jc.base.ApiCallback;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BasePresenter;

/**
 * @Description 修改密码
 * @Auth SongJun
 * @Date 2019/11/28 18:48
 **/
public class ChangePasswordPresenter extends BasePresenter<ChangePasswordCovenant.View, ChangePasswordCovenant.Stores>
        implements ChangePasswordCovenant.Presenter {

    public ChangePasswordPresenter(ChangePasswordCovenant.View view) {
        attachView(view);
    }

    @Override
    public void save() {
        if (StringUtils.isTrimEmpty(mvpView.getOldPassword())) {
            mvpView.showToast("请输入原密码");
            return;
        }
        if (mvpView.getOldPassword().length() < 6 || mvpView.getOldPassword().length() > 16) {
            mvpView.showToast("请输入6~16为字符数字原密码");
            return;
        }
        if (StringUtils.isTrimEmpty(mvpView.getNewPassword())) {
            mvpView.showToast("请输入新密码");
            return;
        }
        if (mvpView.getNewPassword().length() < 6 || mvpView.getNewPassword().length() > 16) {
            mvpView.showToast("请输入6~16为字符数字新密码");
            return;
        }
        if (StringUtils.isTrimEmpty(mvpView.getAgainPassword())) {
            mvpView.showToast("请再输入一次新密码");
            return;
        }
        if (!mvpView.getNewPassword().equals(mvpView.getAgainPassword())) {
            mvpView.showToast("两次密码不一致");
            return;
        }
        mvpView.showLoading("保存中");
        addSubscription(appStores.save(
                mvpView.getOldPassword(),
                mvpView.getNewPassword()),
                new ApiCallback<BaseModel<Object>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<Object> model) {
                        if (model.getResultCode() == 0) {
                            mvpView.hide();
                            mvpView.onSaveSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.hide();
                        mvpView.onSaveFailure(new BaseModel<>(code, msg));
                    }
                });
    }

}